package com.chen.winnerframework.util;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.winnerframework.cqrs.command.query.PageQuery;
import com.chen.winnerframework.domain.dto.PageDto;

/**
 * 分页工具类
 *
 * @author chengy
 * @date 2022/09/27
 */
public class PageUtil {

    /**
     * 当前页 默认值
     */
    private static final int DEFAULT_CURRENT_PAGE = 1;

    /**
     * 页大小 默认值
     */
    private static final int DEFAULT_PAGE_SIZE = 10;

    /**
     * 构造分页对象（CQRS）
     */
    public static <T> Page<T> build(PageQuery<?> pageQuery) {
        Integer currentPage = ObjectUtil.defaultIfNull(pageQuery.getCurrentPage(), DEFAULT_CURRENT_PAGE);
        Integer pageSize = ObjectUtil.defaultIfNull(pageQuery.getPageSize(), DEFAULT_PAGE_SIZE);
        if (pageSize <= 0) {
            pageSize = DEFAULT_PAGE_SIZE;
        }
        return new Page<>(currentPage, pageSize);
    }

    /**
     * 构造分页对象（Dto）
     */
    public static <T> Page<T> build(PageDto dto) {
        Integer currentPage = ObjectUtil.defaultIfNull(dto.getCurrentPage(), DEFAULT_CURRENT_PAGE);
        Integer pageSize = ObjectUtil.defaultIfNull(dto.getPageSize(), DEFAULT_PAGE_SIZE);
        if (pageSize <= 0) {
            pageSize = DEFAULT_PAGE_SIZE;
        }
        return new Page<>(currentPage, pageSize);
    }

}
